package com.effectiv.gooruda.rest.api.controller;

import java.util.List;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.effectiv.gooruda.rest.api.domain.Profile;
import com.effectiv.gooruda.rest.api.domain.Role;
import com.effectiv.gooruda.rest.api.domain.User;
import com.effectiv.gooruda.rest.api.exception.NoDataFoundException;
import com.effectiv.gooruda.rest.api.operation.UserOperations;



@Controller
@RequestMapping("/api/user")
public class UserContoller implements UserOperations {

	@Autowired
	@Qualifier("userServiceImpl")
	private UserOperations operations;

	@RequestMapping(method = RequestMethod.POST)
	@ResponseStatus(value=HttpStatus.CREATED)
	public @ResponseBody
	User save(@Valid @RequestBody User user) {
		return operations.save(user);

	}

	@RequestMapping(value = "/addrole/{userName}/{roleName}", method = RequestMethod.POST)
	public @ResponseBody
	User addRole(@PathVariable("userName") String userName,
			@PathVariable("roleName") String roleName) {
		return operations.addRole(userName, roleName);

	}

	@RequestMapping(value = "/addroles/{userName}/{roleNames}", method = RequestMethod.POST)
	public @ResponseBody
	User addRoles(@PathVariable("userName") String userName,
			@PathVariable("roleNames") List<String> roles) {
		return operations.addRoles(userName, roles);
	}

	@RequestMapping(value = "/authenticate/{userName}/{password}", method = RequestMethod.POST)
	public @ResponseBody
	User authenticate(@PathVariable("userName") String userName,
			@PathVariable("password") String password) {
		return operations.authenticate(userName, password);
	}

	@RequestMapping(value = "/changePassword/{userName}/{oldPassword}/{newPassword}", method = RequestMethod.POST)
	public @ResponseBody
	User changePassword(@PathVariable("userName") String userName,
			@PathVariable("oldPassword") String oldPassword,
			@PathVariable("newPassword") String newPassword) {
		return operations.changePassword(userName, oldPassword, newPassword);
	}

	@RequestMapping(value = "/findbyusername/{userName}", method = RequestMethod.GET)
	public @ResponseBody
	User findByUserName(@PathVariable("userName") String userName) {
		User u = operations.findByUserName(userName);

		if (u == null) {
			throw new NoDataFoundException("User with name - " + userName
					+ " not found.");
		}
		return u;
	}
	
	@RequestMapping(value = "/authenticatewithnewsession/{userName}/{password}", method = RequestMethod.GET)
	public @ResponseBody User authenticateAndStartSession(@PathVariable("userName") String userName, 
			@PathVariable("password") String password) {
		return operations.authenticateAndStartSession(userName, password);
	}
	
	@RequestMapping(value = "/addorupdateprofile/{userName}", method = RequestMethod.PUT)
	public @ResponseBody User addOrUpdateProfile(String userName, Profile profile) {
		return operations.addOrUpdateProfile(userName, profile);
	}
	
	@RequestMapping(value="/{userName}" , method = RequestMethod.DELETE)
	public void deleteUser(@PathVariable("userName") String userName) {
		operations.deleteUser(userName);

	}
	@RequestMapping(value="/findall" , method = RequestMethod.GET)
	public @ResponseBody List<User> findAll(@RequestParam(value="pageNo", defaultValue="0", required=false) int pageNo, 
			@RequestParam(value="pageSize", defaultValue="0", required=false) int pageSize) {
		return operations.findAll(pageNo, pageSize);
	}
	
	@RequestMapping(value="/count" , method = RequestMethod.GET)
	public @ResponseBody long count() {
		return operations.count();
	}
	
	@RequestMapping(value="/findlockedusers" , method = RequestMethod.GET)
	public @ResponseBody List<User> findLockedUsers(@RequestParam(value="pageNo", defaultValue="0", required=false) int pageNo, 
			@RequestParam(value="pageSize", defaultValue="0", required=false) int pageSize) {
		return operations.findLockedUsers(pageNo, pageSize);
	}
	
	@RequestMapping(value="/countlockedusers" , method = RequestMethod.GET)
	public @ResponseBody long countLockedUsers() {
		return operations.countLockedUsers();
	}
	
	@RequestMapping(value="/findrolesforuser" , method = RequestMethod.GET)
	public @ResponseBody List<Role> findRolesForUser(String userName) {
		return operations.findRolesForUser(userName);
	}
	
	@RequestMapping(value="/findone/{id}" , method = RequestMethod.GET)
	public @ResponseBody User findOne(@PathVariable String id) {
		return operations.findOne(id);
	}
	
	@RequestMapping(value="/findbyemail/{email}" , method = RequestMethod.GET)
	public @ResponseBody User findByEmail(@PathVariable String email) {
		return operations.findByEmail(email);
	}
	
	@RequestMapping(value="/lock/{userName}" , method = RequestMethod.PUT)
	public @ResponseBody User lock(@PathVariable String userName) {
		return operations.lock(userName);
	}
	
	@RequestMapping(value="/unlock/{userName}" , method = RequestMethod.PUT)
	public  @ResponseBody User unlock(@PathVariable String userName) {
		return operations.unlock(userName);
	}
	
	@RequestMapping(value="/removeallroles/{userName}" , method = RequestMethod.PUT)
	public User removeAllRoles(@PathVariable String userName) {
		return operations.removeAllRoles(userName);
	}
	
	@RequestMapping(value="/updateemail/{userName}/{newEmail}" , method = RequestMethod.PUT)
	public User updateEmail(@PathVariable("userName") String userName, @PathVariable("newEmail") String newEmail) {
		return operations.updateEmail(userName, newEmail);
	}

}
